import {
	objDeepCopy,
	getDateString
} from "@/assets/js/common.js";
import commonObject from "@/assets/js/xm.js";
let Controller = {
	created() {
		this.init();
	},
	mounted() {

	},
	methods: {
		init() {
			var that = this;
			var pramas = {
				condition: {
					status: undefined
				},
				pageIndex: 1
			}
			this.getTableList(this, pramas);
			this.getMaterialOption(this, 'material', 1);
			//
			this.getCountFunction(this, undefined, 0);
			this.getCountFunction(this, '10', 1);
			this.getCountFunction(this, '20', 2);
			this.getCountFunction(this, '30', 3);
		},
		//获取数量
		getCountFunction(that, status, index) {
			var apiTest = this.apiTest;
			status = status || undefined;
			this.$ajax({
				method: 'post',
				// url:'192.168.24.167:8031/supplierQuote/getCount',
				url: apiTest + '/supplier/supplierQuote/getCount',
				data: {
					status: status
				}
			}).then((res) => {
				var data = res.data;
				if (data.result) {
					that.Model.configArr[index].count = data.result;
				} else {
		
				}
		
			});
		},
		//获取列表数据
		getTableList(that, pramas) {
			var apiTest = this.apiTest;
			pramas = pramas || {};
			this.$ajax({
				method: 'post',
				url: apiTest + '/supplier/supplierQuote/list',
				data: pramas
			}).then((res) => {
				console.log(res);
				that.Model.tableData = []
				var data = res.data;
				if (data.status == 200) {
					if (data.result) {
						var pagination = commonObject.paginationFunction(data.result);
						that.Model.paginationObject = objDeepCopy(pagination);
						var result = data.result.result;
						var trObj = {};
						for (var i = 0; i < result.length; i++) {
							trObj = objDeepCopy(result[i]);
							if(result[i].createUser){
								trObj.createName = result[i].createUser.userName //创建人
							}
							if(result[i].materialDictionary){
								trObj.showMaterial = result[i].materialDictionary.value //材质
							}
							if(result[i].statusDictionary){
								trObj.showStatus = result[i].statusDictionary.value //状态
							}
							that.Model.tableData.push(trObj);
						}
					}
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		//*********************************************************************************获取详情
		detail(that, id) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url: apiTest + '/supplier/supplierQuote/' + id
			}).then((res) => {
				var result = res.data.result;
				console.log(result);
				if (result) {
					if (result.material == 10) {
						that.Model.materialShow = '红18k金'
					} else if (result.material == 20) {
						that.Model.materialShow = '分色18k金'
					} else if (result.material == 30) {
						that.Model.materialShow = '黄18k金'
					} else {

					}
					that.Model.dialogAdd.material = result.material
					that.Model.dialogAdd.supplierPrice = result.supplierPrice;
					that.Model.dialogAdd.exchangePrice = result.exchangePrice;
					that.Model.dialogAdd.id = result.id;
				}
				that.Model.dialogAlert = true;
			});
		},
		//新增方法（修改方法）
		add(that, pramas, type) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: type,
				url: apiTest + '/supplier/supplierQuote',
				data: pramas
			}).then((res) => {
				console.log(res);
				var data = res.data;
				if (data) {
					if (data.status == 200) {
						that.tips(that, 'success', '操作成功');
						that.Model.searchObject.condition.status = that.Model.cardStatus;
						this.getTableList(that, that.Model.searchObject);
						setTimeout(function() {
							that.Model.dialogAlert = false;
						}, 1000)
					} else {
						that.tips(that, 'warning', data.msg);
					}

				}

			});
		},
		//点击保存
		save() {
			
			// this.dialogAlert = false;
			var pramas = this.Model.dialogAdd;
			if (this.Model.materialShow == '红18k金') {
				this.Model.dialogAdd.material = 10
			} else if (this.Model.materialShow == '分色18k金') {
				this.Model.dialogAdd.material = 20
			} else if (this.Model.materialShow == '黄18k金') {
				this.Model.dialogAdd.material = 30
			}
			if (this.Model.dialogAdd.id) {
				this.add(this, pramas, 'put');
			} else {
				pramas.material = this.Model.materialShow
				this.add(this, pramas, 'post');
			}

		},
		newList() { //*********************************************************点击新建
			this.Model.dialogAlert = true;
			this.Model.dialogAdd = {};
		},
		//********************************************************************点击修改
		handleClickChangeList(data) {
			var id = data.id;
			this.detail(this, id);

		},
		//*****************************************************获取下拉选项
		getMaterialOption(that, type, index) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/base/systemDictionary/getByType/' + type,
				data: {}
			}).then((res) => {
				var data = res.data;
				if (data) {
					var stampArr = [{
						label: '全部',
						value: undefined
					}];
					var result = data.result;
					for (var i = 0; i < result.length; i++) {
						stampArr.push({
							label: result[i].value,
							value: result[i].key
						});
					}
					that.Model.filterOption.items[index].data = stampArr;
					that.Model.selectData = stampArr;
				}
			});
		},
		//点击卡片
		cardEmit(card) {
			var value = card.value;
			var pramas = {
				condition: {
					status: value
				}
			}
			this.getTableList(this, pramas);
		
		},
		selectChange() { //*****************************************************下拉选项

		},
		handleSizeChange(val) {
			console.log(`每页 ${val} 条`);
			var pramas = this.Model.searchObject;
			pramas.pageSize = val;
			this.getTableList(this, pramas);
		},
		handleCurrentChange(val) {
			var that = this;
			this.Model.currentPage = val;
			var pramas = this.Model.searchObject;
			pramas.pageIndex = val;
			this.getTableList(that, pramas)
		},


		handleSelectionChange(data) {
			// 选中的数组
			this.Model.TableSelectData = data;
			var str = '';
			for (var i = 0; i < data.length; i++) {
				str += data[i].id + ',';
			}
			str = str.substr(0, str.length - 1);
			this.Model.TableSelectDataStr = str;


		},
		handleClickApproval(data) { //点击审批

		},
		handleClickObsolete(data) { // 点击作废

		},
		batchApproval() { //点击批量审批


		},
		batchExport() { //点击批量导出
			var apiTest = this.apiTest;
			if (this.Model.TableSelectDataStr) {
				window.open(apiTest + '/supplier/common/exportExcel?name=supplierQuote&idList=' + this.Model.TableSelectDataStr);
			} else {
				this.Model.tips(this, 'warning', '请先勾选导出条目');
			}

		},
		selectedHighlight(data) {
			// 如果选中则添加高亮样式
			if (this.Model.TableSelectData.indexOf(data.row) > -1) {
				return {
					"background-color": "#FEF6EE"
				};
				return {}
			}
		},
		showChild(val) {
			var startTime;
			var endTime;
			if (val.createDate) {
				startTime = getDateString(val.createDate[0])
				endTime = getDateString(val.createDate[1])
			} else {
				startTime = undefined;
				endTime = undefined;
			}
			var obj = {
				condition: {
					material: val.material,
					supplierId: val.supplierId,
					status: this.Model.cardStatus,
					startTime: startTime,
					endTime: endTime
				},
				pageIndex: 1,
				pageSize: 10

			}
			this.Model.searchObject = obj;
		},
		toSearch(val) {
			
			var pramas = this.Model.searchObject;
			this.getTableList(this, pramas)

		},
		toClear(val) {
			console.log(val);
			this.Model.searchObject.condition = {
				status: this.Model.cardStatus,
			}
		},
		toExport(val) {
			console.log(val);
		},
		tips(that, type, message) {
			let duration;
			type == 'success' ? duration = 3000 : duration = 10000;
			that.$message({
				message: message,
				type: type,
				duration: duration
			});
		},
	},
	watch: {
		$route: {
			handler(newValue, oldValue) {

			},
			deep: true
		},

	},
	filters: {
		objFilters: function(value) {

		}
	}
}
export default Controller;
